﻿using System;
using System.Collections.Generic;
using Grishko.DataTier;
using System.Linq;
using OutdoorLib.Context;
using AgencyEntityLib.BaseEntity;
using OutdoorLib.BaseEntity.Dictionary;
using AgencyEntityLib.BaseEntity.Dictionary;

namespace OutdoorLib.BaseEntity.WS
{
    public class wsDictionary:EntitySingle
    {

        /// <summary>
        /// Вернуть подерживаемые типы справочников
        /// </summary>
        /// <returns></returns>
        public static List<DictionaryCategoryItem> GetDictionaryCategoryList()
        {
            List<DictionaryCategoryItem> res = new List<DictionaryCategoryItem>();
            res.AddRange(GetProviderDictionaryCategoryList());
            res.AddRange(GetAgencyDictionaryCategoryList());
            return res;

        }

        /// <summary>
        /// вернуть допустимые типы системных справочников
        /// </summary>
        /// <returns></returns>
        public static List<DictionaryCategoryItem> GetProviderDictionaryCategoryList()
        {
            return  AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.CategoryList.ToList <DictionaryCategoryItem>();             

        }

        /// <summary>
        /// вернуть допустимые типы справочников агентсва
        /// </summary>
        /// <returns></returns>
        public static List<DictionaryCategoryItem> GetAgencyDictionaryCategoryList()
        {

            return AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.CategoryList.ToList<DictionaryCategoryItem>();
            
        }
        
        private static bool IsProviderDictionary(string TypeID)
        {
            DictionaryCategoryItem CategoryType = (DictionaryCategoryItem)AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.CategoryList[TypeID];
            return null != CategoryType;
 
        }

        public static TreeDictionaryItem Create(string TypeID)
        {
            TreeDictionaryItem result = null;
            DictionaryCategoryItem CategoryType = (DictionaryCategoryItem)AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.CategoryList[TypeID];
            if (null != CategoryType)
            {                
                result = CategoryType.CreateTreeDictionaryItem();
                AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.DictionaryList.Add(result);
                AgencyLib_GlobalConstants.CurrentProvider.Save();
                return result;
            }

           CategoryType= (DictionaryCategoryItem)AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.CategoryList[TypeID];
           if (null != CategoryType)
           {             
               result = CategoryType.CreateTreeDictionaryItem();
               AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.DictionaryList.Add(result);
               AgencyLib_GlobalConstants.CurrentAgency.Save();
               return result;

           }


            if (null == CategoryType)
                throw new Exception("Справочник заданной категории не найден");

            return result;
        }
        
        /// <summary>
        /// вернуть список деревянных справочников (системные плюс агентсва)
        /// </summary>
        public static List<TreeDictionaryItem> GetTreeDictionaryList()
        {
            List<TreeDictionaryItem> res = new List<TreeDictionaryItem>();
            res.AddRange(AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.DictionaryList.ToArray<TreeDictionaryItem>());
            res.AddRange(AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.DictionaryList.ToArray<TreeDictionaryItem>());
            return res;

        }
        
        /// <summary>
        /// Вернуть типы системных справочников
        /// </summary>
        /// <returns></returns>
        public static List<TreeDictionaryItem> GetProviderTreeDictionaryList()
        {
            List<TreeDictionaryItem> res = new List<TreeDictionaryItem>();
            res.AddRange(AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.DictionaryList.ToArray<TreeDictionaryItem>());            
            return res;

        }

        /// <summary>
        /// справочники относящиеся к текущему агентсву
        /// </summary>
        /// <returns></returns>
        public static List<TreeDictionaryItem> GetAgencyTreeDictionaryList()
        {
            List<TreeDictionaryItem> res = new List<TreeDictionaryItem>();
            res.AddRange(AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.DictionaryList.ToArray<TreeDictionaryItem>());
            return res;

        }

        /// <summary>
        /// вернуть список деревянных справочников (системные плюс агентсва) определенного типа
        /// </summary>
        public static TreeDictionaryItem[] GetTreeDictionaryList(string TypeId)
        {
            List<TreeDictionaryItem> r = GetTreeDictionaryList();
              var result =
                    from
                        n in r
                    where
                         n.TypeID == TypeId
                    select n ;

            return result .ToArray <TreeDictionaryItem>();

        }

       
        /// <summary>
        /// вернуть справочник по ID справочника
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static TreeDictionaryItem GetTreeDictionary(string ID)
        {
            List<TreeDictionaryItem> r = GetTreeDictionaryList();
            foreach (TreeDictionaryItem i in r)
            {
                if (i.ID == ID)
                    return i;
            }
            return null;

        }


        /// <summary>
        /// пытается по типу отыскать справочник 
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static TreeDictionaryItem GetTreeDictionaryItem(string TypeId)
        {
            
            TreeDictionaryItem result = null;
            ///Если существует 
            foreach (TreeDictionaryItem i in GetTreeDictionaryList(TypeId))
            {
                return i;
            }

            return result ; 
        
        }
        
        /// <summary>
        /// Истина - если это справочник провайдера
        /// </summary>
        /// <param name="obj"></param>
        private static bool IsProviderDictionary(TreeDictionaryItem obj)
        {
           
            return (null!= AgencyLib_GlobalConstants.CurrentProvider.Settings .DictionaryInfo.DictionaryList [obj]);

        }

        /// <summary>
        /// Сохранить справочник. 
        /// </summary>
        /// <param name="obj"></param>
        public static void SaveDictionary(TreeDictionaryItem obj)
        {   
            if (null !=AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.DictionaryList[obj.ID])
            {
                AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.DictionaryList.Remove(obj.ID);
                AgencyLib_GlobalConstants.CurrentProvider.Settings.DictionaryInfo.DictionaryList.Add(obj);
                AgencyLib_GlobalConstants.CurrentProvider.SaveWitchChild();

            }
            else
            {
                AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.DictionaryList.Remove(obj.ID);
                AgencyLib_GlobalConstants.CurrentAgency.Settings.DictionaryInfo.DictionaryList.Add(obj);
                wsAgency.SaveAgency();
            }

        }

        /// <summary>
        /// Вернуть справочник по типу
        /// </summary>
        /// <param name="TypeId"></param>
        /// <returns></returns>
        public static TreeDictionaryItem GetTreeDictionaryItemWithTypeID(string TypeId)
        {
            ///Если существует
            foreach (TreeDictionaryItem i in GetTreeDictionaryList(TypeId))
            {
                return i;
            }

            return null;

        }

        /// <summary>
        /// Вернуть справочник по ID
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static TreeDictionaryItem GetTreeDictionaryItemWithID(string ID)
        {
            ///Если существует
            foreach (TreeDictionaryItem i in GetTreeDictionaryList())
            {
                if(i.ID==ID)
                        return i;
            }

            return null;

        }

        
    }
}
